﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://www.codeplex.com/EmlenMud
using System.Runtime.Serialization;
using System;
using System.Threading;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BrainTechLLC.ThreadSafeObjects;

namespace BrainTechLLC
{
    public static class InterpretText
    {
        [MethodVisible]
        public static CommandToPerform ParseInputText(this string input, GameChar chr)
        {
            string[] strings = input.Split(" ".ToCharArray(), 2, StringSplitOptions.None);
            string first = strings[0].ToUpper();
            string rest = (strings.Length) > 1 ? strings[1] : string.Empty;

            List<Command> commands = chr.Abilities.EnabledCommands;
            CommandToPerform command = null;

            for (int n = 0; n < commands.Count; n++)
            {
                Command cmd = commands[n];
                string word = cmd.FirstWords.Find((s) => first.Equals(s, StringComparison.OrdinalIgnoreCase));

                if (word != null && cmd.CheckIsValid(chr, first, rest))
                {
                    command = cmd.CreateCommand(chr, first, rest);
                    break;
                }
            }

            if (command == null)
            {
                for (int n = 0; n < commands.Count; n++)
                {
                    Command cmd = commands[n];
                    string word = cmd.FirstWords.Find((s) => s.IndexOf(first, 0, StringComparison.OrdinalIgnoreCase) == 0);

                    if (word != null && cmd.CheckIsValid(chr, first, rest))
                    {
                        command = cmd.CreateCommand(chr, first, rest);
                        break;
                    }
                }
            }

            return command;
        }
    }
}
